<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
 <head>
  <meta http-equiv="content-type" content="text/html; charset=UTF-8">
  <title>Binds a name to a socket</title>

 </head>
 <body><div class="manualnavbar" style="text-align: center;">
 <div class="prev" style="text-align: left; float: left;"><a href="function.socket-accept.html">socket_accept</a></div>
 <div class="next" style="text-align: right; float: right;"><a href="function.socket-clear-error.html">socket_clear_error</a></div>
 <div class="up"><a href="ref.sockets.html">Socket 函数</a></div>
 <div class="home"><a href="index.html">PHP Manual</a></div>
</div><hr /><div id="function.socket-bind" class="refentry">
 <div class="refnamediv">
  <h1 class="refname">socket_bind</h1>
  <p class="verinfo">(PHP 4 &gt;= 4.1.0, PHP 5)</p><p class="refpurpose"><span class="refname">socket_bind</span> &mdash; <span class="dc-title">Binds a name to a socket</span></p>

 </div>

 <div class="refsect1 description" id="refsect1-function.socket-bind-description">
  <h3 class="title">说明</h3>
  <div class="methodsynopsis dc-description">
   <span class="type">bool</span> <span class="methodname"><strong>socket_bind</strong></span>
    ( <span class="methodparam"><span class="type">resource</span> <code class="parameter">$socket</code></span>
   , <span class="methodparam"><span class="type">string</span> <code class="parameter">$address</code></span>
   [, <span class="methodparam"><span class="type">int</span> <code class="parameter">$port</code><span class="initializer"> = 0</span></span>
  ] )</div>

  <p class="para rdfs-comment">
   Binds the name given in <em><code class="parameter">address</code></em> to the socket
   described by <em><code class="parameter">socket</code></em>. This has to be done before
   a connection is be established using <span class="function"><a href="function.socket-connect.html" class="function">socket_connect()</a></span>
   or <span class="function"><a href="function.socket-listen.html" class="function">socket_listen()</a></span>.
  </p>
 </div>


 <div class="refsect1 parameters" id="refsect1-function.socket-bind-parameters">
  <h3 class="title">参数</h3>
  <p class="para">
   <dl>

    
     <dt>
<em><code class="parameter">socket</code></em></dt>

     <dd>

      <p class="para">
       A valid socket resource created with <span class="function"><a href="function.socket-create.html" class="function">socket_create()</a></span>.
      </p>
     </dd>

    
    
     <dt>
<em><code class="parameter">address</code></em></dt>

     <dd>

      <p class="para">
       If the socket is of the <strong><code>AF_INET</code></strong> family, the
       <em><code class="parameter">address</code></em> is an IP in dotted-quad notation
       (e.g. <em>127.0.0.1</em>).
      </p>
      <p class="para">
       If the socket is of the <strong><code>AF_UNIX</code></strong> family, the
       <em><code class="parameter">address</code></em> is the path of a
       Unix-domain socket (e.g. <var class="filename">/tmp/my.sock</var>).
      </p>
     </dd>

    
    
     <dt>
<em><code class="parameter">port</code></em> (Optional)</dt>

     <dd>

      <p class="para">
       The <em><code class="parameter">port</code></em> parameter is only used when
       binding an <strong><code>AF_INET</code></strong> socket, and designates
       the port on which to listen for connections.
      </p>
     </dd>

    
   </dl>

  </p>
 </div>


 <div class="refsect1 returnvalues" id="refsect1-function.socket-bind-returnvalues">
  <h3 class="title">返回值</h3>
  <p class="para">
   成功时返回 <strong><code>TRUE</code></strong>， 或者在失败时返回 <strong><code>FALSE</code></strong>。
  </p>
  <p class="para">
   The error code can be retrieved with <span class="function"><a href="function.socket-last-error.html" class="function">socket_last_error()</a></span>.
   This code may be passed to <span class="function"><a href="function.socket-strerror.html" class="function">socket_strerror()</a></span> to get a
   textual explanation of the error.
  </p>
 </div>


 <div class="refsect1 examples" id="refsect1-function.socket-bind-examples">
  <h3 class="title">范例</h3>
  <p class="para">
   <div class="example" id="example-4491">
    <p><strong>Example #1 Using <span class="function"><strong>socket_bind()</strong></span> to set the source address</strong></p>
    <div class="example-contents">
<div class="phpcode"><code><span style="color: #000000">
<span style="color: #0000BB">&lt;?php<br /></span><span style="color: #FF8000">//&nbsp;Create&nbsp;a&nbsp;new&nbsp;socket<br /></span><span style="color: #0000BB">$sock&nbsp;</span><span style="color: #007700">=&nbsp;</span><span style="color: #0000BB">socket_create</span><span style="color: #007700">(</span><span style="color: #0000BB">AF_INET</span><span style="color: #007700">,&nbsp;</span><span style="color: #0000BB">SOCK_STREAM</span><span style="color: #007700">,&nbsp;</span><span style="color: #0000BB">SOL_TCP</span><span style="color: #007700">);<br /><br /></span><span style="color: #FF8000">//&nbsp;An&nbsp;example&nbsp;list&nbsp;of&nbsp;IP&nbsp;addresses&nbsp;owned&nbsp;by&nbsp;the&nbsp;computer<br /></span><span style="color: #0000BB">$sourceips</span><span style="color: #007700">[</span><span style="color: #DD0000">'kevin'</span><span style="color: #007700">]&nbsp;&nbsp;&nbsp;&nbsp;=&nbsp;</span><span style="color: #DD0000">'127.0.0.1'</span><span style="color: #007700">;<br /></span><span style="color: #0000BB">$sourceips</span><span style="color: #007700">[</span><span style="color: #DD0000">'madcoder'</span><span style="color: #007700">]&nbsp;=&nbsp;</span><span style="color: #DD0000">'127.0.0.2'</span><span style="color: #007700">;<br /><br /></span><span style="color: #FF8000">//&nbsp;Bind&nbsp;the&nbsp;source&nbsp;address<br /></span><span style="color: #0000BB">socket_bind</span><span style="color: #007700">(</span><span style="color: #0000BB">$sock</span><span style="color: #007700">,&nbsp;</span><span style="color: #0000BB">$sourceips</span><span style="color: #007700">[</span><span style="color: #DD0000">'madcoder'</span><span style="color: #007700">]);<br /><br /></span><span style="color: #FF8000">//&nbsp;Connect&nbsp;to&nbsp;destination&nbsp;address<br /></span><span style="color: #0000BB">socket_connect</span><span style="color: #007700">(</span><span style="color: #0000BB">$sock</span><span style="color: #007700">,&nbsp;</span><span style="color: #DD0000">'127.0.0.1'</span><span style="color: #007700">,&nbsp;</span><span style="color: #0000BB">80</span><span style="color: #007700">);<br /><br /></span><span style="color: #FF8000">//&nbsp;Write<br /></span><span style="color: #0000BB">$request&nbsp;</span><span style="color: #007700">=&nbsp;</span><span style="color: #DD0000">'GET&nbsp;/&nbsp;HTTP/1.1'&nbsp;</span><span style="color: #007700">.&nbsp;</span><span style="color: #DD0000">"\r\n"&nbsp;</span><span style="color: #007700">.<br />&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="color: #DD0000">'Host:&nbsp;example.com'&nbsp;</span><span style="color: #007700">.&nbsp;</span><span style="color: #DD0000">"\r\n\r\n"</span><span style="color: #007700">;<br /></span><span style="color: #0000BB">socket_write</span><span style="color: #007700">(</span><span style="color: #0000BB">$sock</span><span style="color: #007700">,&nbsp;</span><span style="color: #0000BB">$request</span><span style="color: #007700">);<br /><br /></span><span style="color: #FF8000">//&nbsp;Close<br /></span><span style="color: #0000BB">socket_close</span><span style="color: #007700">(</span><span style="color: #0000BB">$sock</span><span style="color: #007700">);<br /><br /></span><span style="color: #0000BB">?&gt;</span>
</span>
</code></div>
    </div>

   </div>
  </p>
 </div>


 <div class="refsect1 notes" id="refsect1-function.socket-bind-notes">
  <h3 class="title">注释</h3>
  <blockquote class="note"><p><strong class="note">Note</strong>: 
   <p class="para">
    This function must be used on the socket before
    <span class="function"><a href="function.socket-connect.html" class="function">socket_connect()</a></span>.
   </p>
  </p></blockquote>
  <blockquote class="note"><p><strong class="note">Note</strong>: 
   <p class="para">
    Windows 9x/ME compatibility note:
    <span class="function"><a href="function.socket-last-error.html" class="function">socket_last_error()</a></span> may return an invalid error code
    if trying to bind the socket to a wrong address that does not belong to
    your machine.
   </p>
  </p></blockquote>
 </div>


 <div class="refsect1 seealso" id="refsect1-function.socket-bind-seealso">
  <h3 class="title">参见</h3>
  <p class="para">
   <ul class="simplelist">
    <li class="member"><span class="function"><a href="function.socket-connect.html" class="function" rel="rdfs-seeAlso">socket_connect()</a> - Initiates a connection on a socket</span></li>
    <li class="member"><span class="function"><a href="function.socket-listen.html" class="function" rel="rdfs-seeAlso">socket_listen()</a> - Listens for a connection on a socket</span></li>
    <li class="member"><span class="function"><a href="function.socket-create.html" class="function" rel="rdfs-seeAlso">socket_create()</a> - 创建一个套接字（通讯节点）</span></li>
    <li class="member"><span class="function"><a href="function.socket-last-error.html" class="function" rel="rdfs-seeAlso">socket_last_error()</a> - Returns the last error on the socket</span></li>
    <li class="member"><span class="function"><a href="function.socket-strerror.html" class="function" rel="rdfs-seeAlso">socket_strerror()</a> - Return a string describing a socket error</span></li>
   </ul>
  </p>
 </div>


</div><hr /><div class="manualnavbar" style="text-align: center;">
 <div class="prev" style="text-align: left; float: left;"><a href="function.socket-accept.html">socket_accept</a></div>
 <div class="next" style="text-align: right; float: right;"><a href="function.socket-clear-error.html">socket_clear_error</a></div>
 <div class="up"><a href="ref.sockets.html">Socket 函数</a></div>
 <div class="home"><a href="index.html">PHP Manual</a></div>
</div></body></html>
